/*
 * Filename    : Entity_Benda.java
 * Projectname : RamaShinta
 * Date        : Dec 26, 2012 , 11:20:19 AM 
 * 
 */

package com.amikomgamedev.ramashinta.entity;

import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.ui.activity.BaseGameActivity;

import com.amikomgamedev.ramashinta.Utils;
import com.amikomgamedev.ramashinta.graphic.Graphic_Data;
import com.amikomgamedev.ramashinta.graphic.Graphic_Detach;

public class Entity_Benda implements Graphic_Data
{
	public Sprite				sprBenda,sprChoco,sprRacun,sprKayu,sprApi,sprBoneka,sprPoisonLiquid,
								sprBeeHive,sprMetal,sprBorgol,sprDumbell,sprQuiver;
	public Sprite 				sprRose;
	public AnimatedSprite 		sprWingerHeart;

	
	private Rectangle			rectTanah;
	private Graphic_Detach		graphic;
	private BaseGameActivity	activity;
	PhysicsHandler				handler;

	final float					xLeft	= Utils.getRatioW(200);
	final float					x1		= Utils.getRatioW(300);
	final float					xMiddle	= Utils.getRatioW(400);
	final float					x2		= Utils.getRatioW(500);
	final float					xRight	= Utils.getRatioW(600);

	private PoolBenda			poolBenda;
	private Sprite				sprCoba;
	private PoolBenda			poolChoco;
	private Sprite				sprCoba2;
	private PoolBenda			poolRacun;
	private Sprite				sprCoba3;
	private PoolBenda			poolKayu;
	private Sprite				sprCoba4;
	private PoolBenda			poolApi;
	private Sprite				sprCoba5;
	private PoolBenda			poolBoneka;
	private Sprite				sprCoba6;
	private PoolBenda			poolPoisonLQ;
	private Sprite				sprCoba7;
	private PoolBenda			poolRose;
	private PoolBenda			poolWingerHeart;
	private PoolBenda			poolBeeHive;
	private PoolBenda			poolMetal;
	private PoolBenda			poolBorgol;
	private PoolBenda			poolDumbell;
	private PoolBenda			poolQuiver;

	public float				gerakan;
	public float				speedUp;
	public float				speedMed;
	float						x, y;
	private int rndmSpr;
	public Sprite sprRandom;
	private int getSprlove = 0;
	private int getSprChoco = 1;
	private int getSprRacun= 2;
	private int getSprKayu = 3;
	private int getSprApi = 4;
	private int getSprBoneka = 5;
	private int getSprpoisonliquid = 6;
	private int getSprBeeHive = 7;
	private int getSprMetal = 8;
	private int getSprRose = 9;
	private int getSprWingerHeart = 10;
	private int getSprBorgol =11;
	private int getSprDumbell = 12;
	private int getSprQuiver = 13;

	public Entity_Benda(float posX, float posY, int rndSpr, Graphic_Detach pGraphic, BaseGameActivity pActivty)
	{
		activity = pActivty;
		graphic = pGraphic;
		rndmSpr = rndSpr;
		
		switch (rndmSpr)
		{
			case 0:
			{
				poolBenda = new PoolBenda(posX, posY, Utils.getRatio(ITEM_LOVE_WIDTH), Utils.getRatio(ITEM_LOVE_HEIGHT), activity,
						graphic.reg_Item_Love);
				sprCoba = poolBenda.obtainPoolItem();
				
				sprBenda = new Sprite(posX, posY, Utils.getRatio(ITEM_LOVE_WIDTH), Utils.getRatio(ITEM_LOVE_HEIGHT),
						graphic.reg_Item_Love, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprBenda);
				gerakan = posY;
				x = posX;
				y = posY;
				
				
					handler = new PhysicsHandler(sprBenda);
					sprBenda.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprBenda);
				
			}
				break;
			case 1:
			{
				poolChoco = new PoolBenda(posX, posY, Utils.getRatio(ITEM_CHOCO_WIDTH), Utils.getRatio(ITEM_CHOCO_HEIGHT), activity, 
						graphic.reg_Item_Choco);
				sprCoba2 = poolChoco.obtainPoolItem();
				
				sprChoco =  new Sprite(posX, posY, Utils.getRatio(ITEM_CHOCO_WIDTH), Utils.getRatio(ITEM_CHOCO_HEIGHT),
						graphic.reg_Item_Choco, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprChoco);
				gerakan = posY;
				x = posX;
				y = posY;
				
					handler = new PhysicsHandler(sprChoco);
					sprChoco.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprChoco);
				
			}
				break;
			case 2:
			{
				poolRacun = new PoolBenda(posX, posY, Utils.getRatio(ITEM_RACUN_WIDTH), Utils.getRatio(ITEM_RACUN_HEIGHT), activity, 
						graphic.reg_Item_Racun);
				sprCoba3 = poolRacun.obtainPoolItem();
				
				sprRacun =  new Sprite(posX, posY, Utils.getRatio(ITEM_RACUN_WIDTH), Utils.getRatio(ITEM_RACUN_HEIGHT),
						graphic.reg_Item_Racun, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprRacun);
				gerakan = posY;
				x = posX;
				y = posY;
				
				
				
					handler = new PhysicsHandler(sprRacun);
					sprRacun.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprRacun);
				
			}
				break;
			case 3:
			{
				poolKayu = new PoolBenda(posX, posY, Utils.getRatio(ITEM_KAYU_WIDTH), Utils.getRatio(ITEM_KAYU_HEIGHT), activity, 
						graphic.reg_Item_Kayu);
				sprCoba4 = poolKayu.obtainPoolItem();
				
				sprKayu =  new Sprite(posX, posY, Utils.getRatio(ITEM_KAYU_WIDTH), Utils.getRatio(ITEM_KAYU_HEIGHT),
						graphic.reg_Item_Kayu, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprKayu);
				gerakan = posY;
				x = posX;
				y = posY;
				
				
				
					handler = new PhysicsHandler(sprKayu);
					sprKayu.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprKayu);
				
			}
				break;
			case 4:
			{
				poolApi = new PoolBenda(posX, posY, Utils.getRatio(ITEM_API_WIDTH), Utils.getRatio(ITEM_API_HEIGHT), activity, 
						graphic.reg_Item_Api);
				sprCoba5 = poolApi.obtainPoolItem();
				
				sprApi =  new Sprite(posX, posY, Utils.getRatio(ITEM_API_WIDTH), Utils.getRatio(ITEM_API_HEIGHT),
						graphic.reg_Item_Api, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprApi);
				gerakan = posY;
				x = posX;
				y = posY;
				
				
				
					handler = new PhysicsHandler(sprApi);
					sprApi.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprApi);
				
			}
				break;
			case 5:
			{
				Utils.TRACE("terserah");
				poolBoneka = new PoolBenda(posX, posY, Utils.getRatio(ITEM_BONEKA_WIDTH), Utils.getRatio(ITEM_BONEKA_HEIGHT), activity, 
						graphic.reg_Item_Boneka);
				sprCoba6 = poolBoneka.obtainPoolItem();
				
				sprBoneka =  new Sprite(posX, posY, Utils.getRatio(ITEM_BONEKA_WIDTH), Utils.getRatio(ITEM_BONEKA_HEIGHT),
						graphic.reg_Item_Boneka, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprBoneka);
				gerakan = posY;
				x = posX;
				y = posY;
				
				
				
					handler = new PhysicsHandler(sprBoneka);
					sprBoneka.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprBoneka);
				
			}
				break;
			case 6:
			{
				
				poolPoisonLQ = new PoolBenda(posX, posY, Utils.getRatio(ITEM_POISONLQD_WIDTH), Utils.getRatio(ITEM_POISONLQD_WIDTH), activity, 
						graphic.reg_Item_PoisonLQ);
				poolPoisonLQ.obtainPoolItem();
				
				sprPoisonLiquid =  new Sprite(posX, posY, Utils.getRatio(ITEM_POISONLQD_WIDTH), Utils.getRatio(ITEM_POISONLQD_WIDTH),
						graphic.reg_Item_PoisonLQ, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprPoisonLiquid);
				gerakan = posY;
				x = posX;
				y = posY;
				
				
				
					handler = new PhysicsHandler(sprPoisonLiquid);
					sprPoisonLiquid.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprPoisonLiquid);
				
			}
			break;

			
		
			case 7:
			{
				poolBeeHive = new PoolBenda(posX, posY, Utils.getRatio(ITEM_BEEHIVE_WIDTH), Utils.getRatio(ITEM_BEEHIVE_HEIGHT), activity, 
						graphic.reg_Item_BeeHive);
				poolBeeHive.obtainPoolItem();
				
				sprBeeHive =  new Sprite(posX, posY, Utils.getRatio(ITEM_BEEHIVE_WIDTH), Utils.getRatio(ITEM_BEEHIVE_HEIGHT),
						graphic.reg_Item_BeeHive, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprBeeHive);
				gerakan = posY;
				x = posX;
				y = posY;
				
					handler = new PhysicsHandler(sprBeeHive);
					sprBeeHive.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprBeeHive);
			}
			break;
			case 8:
			{
				poolMetal = new PoolBenda(posX, posY, Utils.getRatio(ITEM_METAL_WIDTH), Utils.getRatio(ITEM_METAL_HEIGHT), activity, 
						graphic.reg_Item_Metal);
				poolMetal.obtainPoolItem();
				sprMetal =  new Sprite(posX, posY, Utils.getRatio(ITEM_METAL_WIDTH), Utils.getRatio(ITEM_METAL_HEIGHT),
						graphic.reg_Item_Metal, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprMetal);
				gerakan = posY;
				x = posX;
				y = posY;
				
					handler = new PhysicsHandler(sprMetal);
					sprMetal.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprMetal);
			}
			break;
			case 9:
			{
				poolRose = new PoolBenda(posY, posY, Utils.getRatio(ITEM_ROSE_WIDTH), Utils.getRatio(ITEM_ROSE_HEIGHT), activity, graphic.reg_Item_Rose);
				poolRose.obtainPoolItem();
				sprRose = new Sprite(posX, posY,Utils.getRatio(ITEM_ROSE_WIDTH), Utils.getRatio(ITEM_ROSE_HEIGHT), graphic.reg_Item_Rose, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprRose);
				gerakan = posY;
				x = posX;
				y = posY;
				
			}
			break;
			
			case 10:
			{
				poolWingerHeart = new PoolBenda(posY, posY, Utils.getRatio(ITEM_WINGER_HEART_WIDTH), Utils.getRatio(ITEM_WINGER_HEART_HEIGHT), activity, graphic.reg_Item_Rose);
//				sprCoba8 = poolRose.obtainPoolItem();
				sprWingerHeart = new AnimatedSprite(posX, posY,Utils.getRatio(ITEM_WINGER_HEART_WIDTH), Utils.getRatio(ITEM_WINGER_HEART_HEIGHT), graphic.reg_Item_WingerHeart, activity.getVertexBufferObjectManager());
				sprWingerHeart.animate(50);
				graphic.spr_Gameplay_bg.attachChild(sprWingerHeart);
				gerakan = posY;
				x = posX;
				y = posY;
			}	
			break;
			
			case 11:
			{
				poolBorgol = new PoolBenda(posX, posY, Utils.getRatio(ITEM_BORGOL_WIDTH), Utils.getRatio(ITEM_BORGOL_HEIGHT), activity, 
						graphic.reg_Item_Borgol);
				poolBorgol.obtainPoolItem();
				sprBorgol =  new Sprite(posX, posY, Utils.getRatio(ITEM_BORGOL_WIDTH), Utils.getRatio(ITEM_BORGOL_HEIGHT),
						graphic.reg_Item_Borgol, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprBorgol);
				gerakan = posY;
				x = posX;
				y = posY;
				
					handler = new PhysicsHandler(sprBorgol);
					sprBorgol.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprBorgol);
			}	
			break;
			
			case 12:
			{
				poolDumbell = new PoolBenda(posX, posY, Utils.getRatio(ITEM_DUMBELL_WIDTH), Utils.getRatio(ITEM_DUMBELL_HEIGHT), activity, 
						graphic.reg_Item_Dumbell);
				poolDumbell.obtainPoolItem();
				
				sprDumbell =  new Sprite(posX, posY, Utils.getRatio(ITEM_DUMBELL_WIDTH), Utils.getRatio(ITEM_DUMBELL_HEIGHT),
						graphic.reg_Item_Dumbell, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprDumbell);
				gerakan = posY;
				x = posX;
				y = posY;
				
					handler = new PhysicsHandler(sprDumbell);
					sprDumbell.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprDumbell);
				
			}		
		break;
			case 13:
			{
//				poolQuiver = new PoolBenda(posX, posY, Utils.getRatio(ITEM_QUIVER_WIDTH), Utils.getRatio(ITEM_QUIVER_HEIGHT), activity, 
//						graphic.reg_Item_Quiver);
//				poolQuiver.obtainPoolItem();
//				
//				sprQuiver =  new Sprite(posX, posY, Utils.getRatio(ITEM_QUIVER_WIDTH), Utils.getRatio(ITEM_QUIVER_HEIGHT),
//						graphic.reg_Item_Quiver, activity.getVertexBufferObjectManager());
//				graphic.spr_Gameplay_bg.attachChild(sprQuiver);
//				gerakan = posY;
//				x = posX;
//				y = posY;
				
					handler = new PhysicsHandler(sprQuiver);
					sprQuiver.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprQuiver);
			}		
		break;

		}
		getRandomSpr(rndSpr);
		
		
	}

	public Entity_Benda()
	{
		// TODO Auto-generated constructor stub
	}
	
	public Sprite getRandomSpr(int rndSpr){
		if(rndSpr==getSprlove){
			sprRandom = sprBenda;
			return sprRandom;
		}else if(rndSpr==getSprChoco){
			sprRandom = sprChoco;
			return sprRandom;
		}else if(rndSpr==getSprRacun){
			sprRandom = sprRacun;
			return sprRandom;
		}else if(rndSpr==getSprKayu){
			sprRandom = sprKayu;
			return sprRandom;
		}else if(rndSpr==getSprApi){
			sprRandom = sprApi;
			return sprRandom;
		}else if(rndSpr==getSprBoneka){
			sprRandom = sprBoneka;
			return sprRandom;
		}else if(rndSpr==getSprpoisonliquid){
			sprRandom = sprPoisonLiquid;
			return sprRandom;
		}else if(rndSpr == getSprRose){
			sprRandom = sprRose;
			return sprRandom;
		}else if(rndSpr == getSprWingerHeart){
			sprRandom = sprWingerHeart;
			return sprRandom;
		}else if(rndSpr==getSprMetal){
			sprRandom = sprMetal;
			return sprRandom;
		}else if(rndSpr==getSprBorgol){
			sprRandom = sprBorgol;
			return sprRandom;
		}else if(rndSpr==getSprDumbell){
			sprRandom = sprDumbell;
			return sprRandom;
		}else if(rndSpr==getSprQuiver){
			sprRandom = sprQuiver;
			return sprRandom;
		}else{
			sprRandom = sprBeeHive;
			return sprRandom;
		}
		
	}

	public boolean	gerakKeAtas		= true;

	int		randomPosisiX	= -1;

	public void onUpdate(float pSecondUpdate)
	{
		// System.out.println("benda h = " + sprBenda.getHeight());
		// System.out.println("gerakan = " + gerakan);
		if (gerakKeAtas)
		{
			gerakan -= 5;
			if (sprRandom.getY() < -sprRandom.getHeight())
			{
				randomPosisiX = Utils.GetRandomInRange(1, 5);
				switch (randomPosisiX)
				{
					case 1:
					{
						sprRandom.setX(xLeft);
					}
						break;
					case 2:
					{
						sprRandom.setX(x1);
					}
						break;
					case 3:
					{
						sprRandom.setX(xMiddle);
					}
						break;
					case 4:
					{
						sprRandom.setX(x2);
					}
						break;
					case 5:
					{
						sprRandom.setX(xRight);
					}
						break;
				}
				gerakKeAtas = false;
			}
		} else
		{
			gerakan = Utils.getRatio(1.25f);
			speedMed = Utils.getRatio(1);
			speedUp = Utils.getRatio(2);
		}
		
		if(sprRandom==sprRacun){
			sprRandom.setY(sprRandom.getY() + gerakan +speedMed);
		}else if(sprRandom==sprApi){
			sprRandom.setY(sprRandom.getY() + gerakan +speedMed);
		}else if(sprRandom==sprKayu){
			sprRandom.setY(sprRandom.getY() + gerakan + speedUp);
		}else if(sprRandom==sprBoneka){
			sprRandom.setY(sprRandom.getY() + gerakan + speedMed);
		}else if(sprRandom==sprPoisonLiquid){
			sprRandom.setY(sprRandom.getY() + gerakan + speedMed);

		}else if(sprRandom==sprRose){
			sprRandom.setY(sprRandom.getY() + gerakan + speedMed);
		
		}else if(sprRandom==sprWingerHeart){
			sprRandom.setY(sprRandom.getY() + gerakan + speedMed);
		
		}else if(sprRandom==sprBeeHive){
			sprRandom.setY(sprRandom.getY() + gerakan + speedMed);
		}else if(sprRandom==sprMetal){
			sprRandom.setY(sprRandom.getY() + gerakan + speedMed);

		}else if(sprRandom==sprBorgol){
			sprRandom.setY(sprRandom.getY() + gerakan + speedMed);
		}else if(sprRandom==sprDumbell){
			sprRandom.setY(sprRandom.getY() + gerakan + speedMed);
		}else{
			sprRandom.setY(sprRandom.getY() + gerakan);
		}
		
		
	}

	public void vanishItem()
	{
		sprBenda.detachSelf();
		sprChoco.detachSelf();
		sprRacun.detachSelf();
		sprApi.detachSelf();
		sprKayu.detachSelf();
		sprBoneka.detachSelf();
		sprPoisonLiquid.detachSelf();
		sprRose.detachSelf();
		sprWingerHeart.detachSelf();
		sprBeeHive.detachSelf();
		sprMetal.detachSelf();
		sprBorgol.detachSelf();
		sprDumbell.detachSelf();
		sprQuiver.detachSelf();

	}

}

